#include<iostream>
#include<fcntl.h>
#include<ctype.h>
#include<unordered_map>
#include<unistd.h>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<sys/types.h>
#include<sys/wait.h>
#include<cstring>
using namespace std;


#define COMMAND_SIZE 1024
#define FORMAT "%s@%s:%s$ "


//下面是shell的全局数据
//1.命令行参数表
#define MAXARGC 128
char* g_argv[MAXARGC];
int g_argc = 0;

//2.环境变量表
#define MAX_ENVS 100
char *g_env[MAX_ENVS];
int g_envs = 0;

//3.别名映射表
std::unordered_map<std::string,std::string> alias_list;

//4.关于重定向，我们关心的内容
#define NONE_REDIR 0
#define INPUT_REDIR 1
#define OUTPUT_REDIR 2
#define APPEND_REDIR 3

//#define TrimeSpace(cmd,index) do{
//	while(isspace(*(cmd+index)))
//	{
//		index++;
//	}
//}whil(0);

int redir = NONE_REDIR;
std::string filename;

//for test
char cwd[1024];
char cwdenv[1024];

//最近一次退出码
int lastcode = 0;

const char *GetUserName()
{
	const char *name = getenv("USER");
	return name == NULL ? "None" : name;
}

const char *GetHostName()
{
	const char *hostname = getenv("HOSTNAME");
	return  hostname == NULL ? "None" : hostname;
}

const char *GetPwd()
{
	//const char *pwd = getenv("PWD");
	const char *pwd = getcwd(cwd,sizeof(cwd));
	if(pwd != NULL)
	{
		snprintf(cwdenv,sizeof(cwdenv),"PWD=%s",pwd);
		putenv(cwdenv);
	}
	return pwd == NULL ? "None" : pwd;
}

const char *GetHome()
{
	const char *home = getenv("HOME");
	return home == NULL ? "nullptr" : home;
}
// /  /a/b/c
std::string DirName(const char *pwd)
{
#define SLASH "/"
	const char *home_1 = getenv("HOME");
	std::string home = home_1;
	std::string dir = pwd;
	auto pos = dir.find(home);
	if(pos == std::string::npos) return "BUG?";
	return "~/"+dir.substr(home.size()+1);
}


bool cd()
{

		if(g_argc == 1)
		{
			std::string home = GetHome();
			if(home.empty()) return true;
			chdir(home.c_str());
		}
		else 
		{
			std::string where = g_argv[1];
			//cd ~ 
			if(where == "-")
			{

			}
			else if(where == "~")
			{

			}
			else 
			{
				chdir(where.c_str());
			}
		}
		return true;
}

void InitEnv()
{
	extern char **environ;
	memset(g_env,0,sizeof(g_env));
	g_envs=0;

	//本来是从配置文件中来
	//现在从父shell来
	for(int i=0;environ[i];i++)
	{
		//1.1 申请空间
		g_env[i] = (char*)malloc(strlen(environ[i])+1);
		strcpy(g_env[i],environ[i]);
		g_envs++;
	}
	g_env[g_envs++] = (char*)"ZHEGE = LiCaopeng!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
	g_env[g_envs] = NULL;
	//2.导入
	for(int i=0;g_env[i];i++)
	{
		putenv(g_env[i]);
	}
}

void MakeCommandLine(char Cmd_prompt[],int size)
{
	snprintf(Cmd_prompt,size,FORMAT,GetUserName(),GetHostName(),DirName(GetPwd()).c_str());
	//snprintf(Cmd_prompt,size,FORMAT,GetUserName(),GetHostName(),GetPwd());
}

void PrintCommandPrompt()
{
	char prompt[COMMAND_SIZE];
	MakeCommandLine(prompt,COMMAND_SIZE);
	printf("%s",prompt);
	fflush(stdout);
}

bool GetCommandLine(char *out,int size)
{
	//ls -l -a => 字符串
	char *c = fgets(out,size,stdin);
	if(c == NULL) return false;
	out[strlen(out)-1] = 0;//清理回车\n
	if(strlen(out) == 0) return false;
	return true;
}

//3.命令行分析"ls -a -l"  -> ls    -a     -l
bool CommandParse(char *commandline)
{
#define SEP " "
	g_argc = 0;
	//命令行分析"ls -a -l"  -> ls    -a     -l
	g_argv[g_argc++] =strtok(commandline,SEP);
	
	while(g_argv[g_argc++] = strtok(nullptr,SEP));
	g_argc--;
	return g_argc > 0 ? true : false;
}

void PrintArgv()
{
	for(int i=0;g_argv[i];i++)
	{
		printf("argv[%d]->%s\n",i,g_argv[i]);
	}
	printf("%d\n",g_argc);
}

bool Echo()
{

		if(g_argc == 2)
		{
		
			//echo "hello world"
			//echo $?
			//echo $PATH
			std::string opt = g_argv[1];
			if(opt == "$?")
			{
				std::cout<< lastcode << endl;
				lastcode =0;
				return true;
			}
			else if(opt[0] == '$' )
			{
				std::string env_name = opt.substr(1);
				const char *env_value = getenv(env_name.c_str());
				if(env_value)
				{
					std::cout << env_value << std::endl;
					return true;
				}
			}
			else 
			{
				std::cout << opt << endl;
			}
				
		}
		return true;
}
bool CheckAndExecBuiltin()
{
	std::string cmd = g_argv[0];
	if(cmd == "cd")
	{
		cd();
		return true;
	}
	else if(cmd == "echo")
	{
		Echo();
		return true;
	}
	return false;
}

int Execute()
{
		pid_t id = fork();
		if(id == 0)
		{
			//子进程检测重定向情况
			int fd;
			if(redir == INPUT_REDIR)
			{
				fd = open(filename.c_str(),O_RDONLY);
				if(fd<0) exit(1);
				dup2(fd,0);
				close(fd);
			}
			else if(redir == OUTPUT_REDIR)
			{
				fd = open(filename.c_str(),O_CREAT | O_WRONLY | O_TRUNC,0666);
				if(fd < 0) exit(2);
				dup2(fd,1);
				close(fd);
			}
			else if(redir == APPEND_REDIR)
			{
				fd = open(filename.c_str(),O_CREAT | O_WRONLY | O_APPEND,0666);
				if(fd<0) exit(3);
				dup2(fd,2);
				close(fd);
			}
			else 
			{}
			//child
			execvp(g_argv[0],g_argv);
			exit(1);
		}
		int status = 0;
		pid_t rid = waitpid(id,&status,0);
		if(rid > 0)
		{
			lastcode = WEXITSTATUS(status);
		}
		return 0;
}

void TrimSpace(char cmd[],int &end)
{
	while(isspace(cmd[end]))
	{
		end++;
	}
}

void RedirCheck(char cmd[])
{
	redir = NONE_REDIR;
	filename.clear();
	int start = 0;
	int end = strlen(cmd)-1;

	//"ls -a -l > file.txt"
	while(end > start)
	{
		if(cmd[end] == '<')
		{
			cmd[end] =  0;
			TrimSpace(cmd,end);
			redir = INPUT_REDIR;
			filename = cmd+end;
			break;
		}
		else if(cmd[end] == '>')
		{
			if(cmd[end-1] == '>')
			{
				//>>
				cmd[end-1]=0;
				cmd[end++]==0;
				TrimSpace(cmd,end);
				redir = APPEND_REDIR;
				filename = cmd+end;
				break;
			}
			else 
			{
				//>
				cmd[end++] = 0;
				TrimSpace(cmd,end);
				redir = OUTPUT_REDIR;
				filename = cmd+end;
				break;
			}
		}
		else
		{
			end--;
		}
	}
}
int main()
{
	//shell 启动时，从系统中获取环境变量
	//我们的环境变量从父进程中统一来
	InitEnv();	
	while(true)
	{

		//1.输出命令行提示符
		PrintCommandPrompt();

		//2.获取用户输入的命令
		char commandline[COMMAND_SIZE];
		if(!GetCommandLine(commandline,sizeof(commandline)))
			continue;
		
		printf("echo %s\n",commandline);
		
		//3.重定向分析"ls -a -l > file.txt" -> "ls -a -l"  "file.txt"->判定重定向方式
		RedirCheck(commandline);

		printf("redir：%d    filename:%s\n",redir,filename.c_str());
		//4.命令行分析"ls -a -l"  -> ls    -a     -l
		if(!CommandParse(commandline))
			continue;
		//PrintArgv();i
		
		//4.检测并处理内建命令
		if(CheckAndExecBuiltin())
		{
			continue;
		}


		//5.执行
		Execute();
	}

	//clear();

	return 0;

}
